package com.xw.bo.ks.controller.no;

import com.alibaba.fastjson.JSONObject;
import com.xw.bo.ks.service.AsyncServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
*
* @author： XW -> ~
* @create： 2023/11/8 18:50
*/
@RestController
@RequestMapping("/async")
@CrossOrigin(value = "*",  maxAge = 3600)
public class AsyncController {

    @Autowired
    private AsyncServer server;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public JSONObject asyncTest() throws InterruptedException{
        JSONObject output = new JSONObject();
        long startTime = System.currentTimeMillis();
        int counter = 10;
        for (int i = 0; i<counter ; i++) {
            server.asyncTest(i);
        }
        long endTime = System.currentTimeMillis();
        output.put("msg", "succeed");
        output.put("花费时间: ", endTime-startTime);
        return output;
    }

    @RequestMapping(value = "/test2", method = RequestMethod.GET)
    public JSONObject asyncTest2() throws InterruptedException{
        JSONObject output = new JSONObject();
        long startTime = System.currentTimeMillis();
        int counter = 10;
        for (int i = 0; i<counter ; i++) {
            server.asyncTest(i);
        }
        long endTime = System.currentTimeMillis();
        output.put("msg", "succeed");
        output.put("花费时间: ", endTime-startTime);
        return output;
    }


    @GetMapping(value = "/whileTest")
    public String setUserId(Integer uid) {
        server.whileTest(1);
        return "ok";
    }

}
